package com.binfenhulian.chinaenergy.ces.utils.image;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class ImageUtil
{
  public static int widthdist = 450;

  public static int heightdist = 450;

  public static void reduceImg_byWidth(File srcfile, int widthdist) {
    try {
      BufferedImage image = ImageIO.read(srcfile);
      int heightdist = image.getHeight() * widthdist / image.getWidth();

      Image src = ImageIO.read(srcfile);

      BufferedImage tag = new BufferedImage(widthdist, heightdist, 1);

      tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, 1), 0, 0, null);

//      FileOutputStream out = new FileOutputStream(srcfile.getPath());
//      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//      encoder.encode(tag);
//      out.close();
      ImageIO.write(tag, srcfile.getName(), srcfile );
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void reduceImg_byHeight(File srcfile, int heightdist) {
    try {
      BufferedImage image = ImageIO.read(srcfile);
      int widthdist = image.getWidth() * heightdist / image.getHeight();

      Image src = ImageIO.read(srcfile);

      BufferedImage tag = new BufferedImage(widthdist, heightdist, 1);

      tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, 1), 0, 0, null);

//      FileOutputStream out = new FileOutputStream(srcfile.getPath());
//      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//      encoder.encode(tag);
//      out.close();

      ImageIO.write(tag, srcfile.getName(), srcfile );
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}